//
//  PinPinWeiTableViewCell.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/30.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class PinPinWeiTableViewCell: WZC_TableViewCell {

    var model:PinRagModel?{
        
        didSet
        {
            guard let item = model else {
                
                return
            }
            
            self.bgImgView.kf.setImage(with: URL(string: item.pic_url ?? ""))
            
            self.gridView.reloadData()
        }
    }
    
    private lazy var bgImgView : UIImageView = {
        let imageView = UIImageView()
        self.contentView.addSubview(imageView)
        return imageView
    }()
    
    lazy var gridView : UICollectionView = {
        
        let layout = UICollectionViewFlowLayout.init()
        layout.sectionInset = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
        layout.minimumInteritemSpacing = 0.6
        layout.minimumLineSpacing = 1
        let itemHeight:CGFloat =  (kScreenW-10 - 3*8)/4
        layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
        layout.itemSize = CGSize.init(width: itemHeight, height:itemHeight+40)
        
        let collectionView = UICollectionView.init(frame:.zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.clear
        collectionView.register(HotSellCollectionViewCell.self, forCellWithReuseIdentifier: "HotSellCollectionViewCell")
        //        collectionView.isScrollEnabled = false;
        self.contentView.addSubview(collectionView)
        
        return collectionView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.hexColor(hexadecimal: "#f2f2f2")
        
        let imgRate:CGFloat =  1080.0/270.0
        let imgHeight:CGFloat =  (kScreenW-20)/imgRate
        self.bgImgView.snp.makeConstraints { (make) in
            
            
            make.top.equalToSuperview()
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(imgHeight)
        }
        
        gridView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(UIEdgeInsets(top: imgHeight, left: 0, bottom: 0, right: 0))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PinPinWeiTableViewCell:UICollectionViewDelegate,UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return model?.goods_list?.count ?? 0
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:HotSellCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HotSellCollectionViewCell", for: indexPath) as! HotSellCollectionViewCell
        cell.model = model?.goods_list?[indexPath.item]
        return cell
    }
    
}
